package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServiceResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.pojo.Product;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;

@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {
    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;

    public ServiceResponse addCategory(String categoryName,Integer parentId){
        if (StringUtils.isBlank(categoryName) || parentId == null){
            return ServiceResponse.createByErrorMessage("信息错误");
        }
        Category category = new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);

        int flag = categoryMapper.insert(category);
        if (flag > 0){
            return ServiceResponse.createBySuccessMessage("添加成功");
        }
        return ServiceResponse.createByErrorMessage("添加失败");

    }

    public ServiceResponse setCategory(String name,Integer id){
        if (StringUtils.isBlank(name) || id == null){
            return ServiceResponse.createByErrorMessage("信息错误");
        }
        int flag = categoryMapper.updateNameById(id, name);
        if (flag > 0){
            return ServiceResponse.createBySuccessMessage("修改成功");
        }
        return ServiceResponse.createByErrorMessage("修改失败");
    }

    public ServiceResponse<List<Category>> getChildrenParalleCategory(Integer categoryId){
        List<Category> childrenParalleCategory = categoryMapper.getChildrenParalleCategory(categoryId);
        if (CollectionUtils.isEmpty(childrenParalleCategory)){
            logger.info("未找到当前分类的子分类");
        }

        return ServiceResponse.createBySuccess(childrenParalleCategory);
    }

    /**
     * 递归查询本节点以及子节点id
     * @param categoryId
     * @return
     */
    public ServiceResponse selectCategoryAndChildRenById(Integer categoryId){
        Set<Category> categorySet = Sets.newHashSet();
        findChildCategory(categorySet,categoryId);
        List<Integer> categoryIdList = Lists.newArrayList();
        if (categoryId != null){
            for (Category categoryItem:
                 categorySet) {
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ServiceResponse.createBySuccess(categoryIdList);

    }

    private Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category!=null){
            categorySet.add(category);
        }
        List<Category> childrenParalleCategory = categoryMapper.getChildrenParalleCategory(categoryId);
        for (Category categoryItem:
             childrenParalleCategory) {
            findChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }


}
